package com.skytech.chatim.sky.xmlapi.command;


import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.skytech.chatim.sky.xmlapi.WbxClientUtils;

/**
 * Created by Vega on 2015/1/7.
 */
public abstract class XmlApiCommand {

    protected abstract String getCommandType();

    protected abstract void assembleBodyContent(Element bodyContent);



    public String toXML() {
        Document doc = DocumentHelper.createDocument();
        Element bodyContent = doc.addElement("bodyContent");
        assembleBodyContent(bodyContent);
        String body = WbxClientUtils.doc2string(doc, true);
        if (body.startsWith("<bodyContent>")) {
            return body.replaceFirst("<bodyContent>",
                "<bodyContent xsi:type=\"java:" + getCommandType() + "\">");
        } else {
            return body.replaceFirst("<bodyContent/>",
                "<bodyContent xsi:type=\"java:" + getCommandType() + "\"/>");
        }

    }
}
